package org.springframework.howelly.helpers;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import org.springframework.howelly.utils.ReferenceData;

/**
 * A backing bean for the main hotel search form. Encapsulates the criteria needed toString perform a hotel search.
 */
public class SearchCriteria implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * The user-provided search criteria for finding Hotels.
     */
    private String searchString = "";

    /**
     * The maximum page size of the Obra result list
     */
    private int pageSize = 20;

    /**
     * The current page of the Obra result list.
     */
    private int page;

    private String sortBy = "name";

    private String fromString = "";

    private String toString = "";

    private Date from;

    private Date to;

    public String getSortBy() {
	return sortBy;
    }

    public void setSortBy(String sortBy) {
	this.sortBy = sortBy;
    }

    public String getSearchString() {
	return searchString;
    }

    public void setSearchString(String searchString) {
	this.searchString = searchString;
    }

    public int getPageSize() {
	return pageSize;
    }

    public void setPageSize(int pageSize) {
	this.pageSize = pageSize;
    }

    public int getPage() {
	return page;
    }

    public void setPage(int page) {
	this.page = page;
    }

    public void nextPage() {
	page++;
    }

    public void previousPage() {
	page--;
    }

    public void resetPage() {
	page = 0;
	sortBy = "name";
    }

    public String toString() {
	return "[Search Criteria searchString = '" + searchString + "'";
    }

    public Date getFrom() {
	return from;
    }

    public void setFrom(Date from) {
	this.from = from;
    }

    public Date getTo() {
	return to;
    }

    public void setTo(Date to) {
	this.to = to;
    }

    public void setFromString(String sFrom) {
	this.fromString = sFrom;
	try {
	    DateFormat df = ReferenceData.getSimpleDateFormat();
	    setFrom(df.parse(sFrom));
	} catch (ParseException e) {
	    System.out.println("Esta parseando mal fromString");
	    e.printStackTrace();
	}
    }

    public String getToString() {
	return toString;
    }

    public void setToString(String sTo) {
	this.toString = sTo;
	try {
	    DateFormat df = ReferenceData.getSimpleDateFormat();
	    setTo(df.parse(sTo));
	} catch (ParseException e) {
	    System.out.println("Esta parseando mal toString");
	    e.printStackTrace();
	}
    }

    public String getFromString() {
	return fromString;
    }

}